/* 
彩票程序
1. 双色球逻辑实现
2. 模拟需要买多少注彩票才能中

双色球规则：
1.双色球投注区分为红色球号码区和蓝色球号码区，红色球号码区由1-33共三十三个号码组成，
蓝色球号码区由1-16共十六个号码组成。投注时选择6个红色球号码和1个蓝色球号码组成一
注进行单式投注，每注金额人民币2元。
*/

class BallGroup {
    redBalls = [];
    blueBalls = [];
    constructor() {

    }
}

/**
 * 双色球模拟器类
 */
class ShuangSeQiuSimulator {
    /**
     * id 索引
     */
    static idIdx = 0;
    id;
    name;
    winBallGroup = null;
    currentBallGroup = null;
    playCount = 0;
    /**
     * 
     * @param {string} name 实例名称
     */
    constructor(name = "icedog") {
        ShuangSeQiuSimulator.idIdx++;
        this.id = ShuangSeQiuSimulator.idIdx;
        this.name = name;
    }
    /**
     * 数字转文字填充0,如 5->05,15->15保持不变
     * @param {number} num
     * @returns
     */
    fillZero(num) {
        if (num < 10) {
            return "0" + num;
        } else {
            return "" + num;
        }
    }
    /**
     * 获取随机数
     * @param {number} minValue 最小值，默认 0
     * @param {number} maxValue 最大值，默认 10
     * @param {boolean} includeMaxValue 是否包含最大值,默认false
     */
    getRandomNumber(minValue = 0, maxValue = 10, includeMaxValue = false) {
        let rate = maxValue - minValue;
        if (includeMaxValue) {
            rate = rate + 1;
        }
        return Math.floor(Math.random() * rate) + minValue;
    }
    /**
     * 获取红球
     * @param {Array} skipBalls 需要跳过的球，即已经抽出过的球
     */
    getRedBall(skipBalls) {
        //1-33生成，不能重复
        var ball = 0;
        var isExist = true;
        while (isExist) {
            ball = this.getRandomNumber(1, 33, true);
            isExist = skipBalls.some(b => b == ball);
        }
        return ball;
    }
    /**
     * 获取蓝球
     * @param {*} skipBalls 需要跳过的球，即已经抽出过的球
     */
    getBlueBall(skipBalls) {
        //1-16生成，不能重复
        var ball = 0;
        var isExist = true;
        while (isExist) {
            ball = this.getRandomNumber(1, 16, true);
            isExist = skipBalls.some(b => b == ball);
        }
        return ball;
    }
    getBallGroup() {
        var group = { redBalls: [], blueBalls: [] };
        //6红1蓝
        var redIndex = 6;
        while (redIndex-- > 0) {
            var newRedBall = this.getRedBall(group.redBalls);
            group.redBalls.push(newRedBall);
        }
        var newBlueBall = this.getBlueBall(group.blueBalls);
        group.blueBalls.push(newBlueBall);
        return group;
    }
    /**
     * 设置中奖号码
     */
    setWinNumber() {
        //清空数据
        this.winBallGroup = this.getBallGroup();
    }
    autoPlayUntilWin() {
        this.setWinNumber();
        $(".win-num").text(this.getBallGroupStr(this.winBallGroup));
        let isWin = false;
        var t = setInterval(() => {
            this.playCount++;
            $(".play-count").text(this.playCount);
            this.currentBallGroup = this.getBallGroup();
            $(".current-num").text(this.getBallGroupStr(this.currentBallGroup));
            isWin = this.checkIsWin();
            $(".is-win").text(isWin ? "中了！！！" : "没中。。。");
            if (isWin) {
                clearInterval(t);
            }
        }, 0);
    }
    t() {
        let t = setInterval(() => {
            this.playCount++;
            $(".play-count").text(this.playCount);
            if (this.playCount > 10000) {
                clearInterval(t);
            }
        }, 0);
    }
    checkIsWin() {
        var winRedBalls = this.sortBallsAsc(this.winBallGroup.redBalls);
        var winBlueBalls = this.sortBallsAsc(this.winBallGroup.blueBalls);
        var currentRedBalls = this.sortBallsAsc(this.currentBallGroup.redBalls);
        var currentBlueBalls = this.sortBallsAsc(this.currentBallGroup.blueBalls);
        var isWin = winRedBalls.join(',') === currentRedBalls.join(',') && winBlueBalls.join(',') === currentBlueBalls.join(',');
        return isWin;
    }
    sortBallsAsc(balls) {
        return balls.sort((a, b) => a - b);
    }
    getBallGroupStr(record) {
        let redBalls = this.sortBallsAsc(record.redBalls);
        return `${redBalls.map(b => this.fillZero(b)).join(' ')}-${this.fillZero(record.blueBalls[0])}`;
    }
}


const cpt = new ShuangSeQiuSimulator();

$(".btn-start").click(function (e) {
    //cpt.t();
    cpt.autoPlayUntilWin();
})